/**
 * Created by zsyoung on 2017/8/23.
 */


function showFileList() {
    var url = "/cykspark/file/listFilePage";
    $.ajax({
        url: url,
        dataType: 'json',
        success: function (data) {
            paging_now();
            initList(data);
        }
    });
}

function initList(data) {
    var fileTbody = document.getElementById("file_tbody");
    fileTbody.innerHTML = "";
    for (var i = 0; i < data.length; i++) {
        const id=data[i].fileId;
        var fileTr = document.createElement("tr");
        fileTr.setAttribute("class", "file_tr" + i);
        var td1 = document.createElement("td");
        td1.setAttribute("class", "center");
        td1.innerHTML = i+1;

        var td2 = document.createElement("td");
        td2.setAttribute("class", "center");
        td2.innerHTML = data[i].fileName;
        var td3 = document.createElement("td");
        td3.setAttribute("class", "center");
        td3.innerHTML = data[i].size+"KB";
        var td4 = document.createElement("td");
        td4.setAttribute("class", "center");
        var time =data[i].updateTime;
        var transTime = new Date(time);
        td4.innerHTML = transTime.toLocaleDateString()+' '+transTime.toTimeString().substring(0,8);
        var td5 = document.createElement("td");
        td5.setAttribute("class", "center");
        td5.innerHTML = data[i].remark;
        var td6 = document.createElement("td");
        td6.setAttribute("class", "center");
        var button1 = document.createElement("button");
        button1.innerHTML = "删除";
        button1.setAttribute("class", "btn btn-danger btn-small");
        button1.setAttribute("style", "margin:0 5px");
        button1.onclick = function () {
           deleteFile(id);
        };
        var button2 = document.createElement("button");
        button2.innerHTML = "下载";
        button2.setAttribute("class", "btn btn-success btn-small");
        button2.setAttribute("style", "margin:0 5px");
        button2.onclick =function () {
            var url = "/cykspark/file/downloadFile";
            window.location.href=url+"?fileId="+id;
        } ;
        td6.appendChild(button1);
        td6.appendChild(button2);
        fileTr.appendChild(td1);
        fileTr.appendChild(td2);
        fileTr.appendChild(td3);
        fileTr.appendChild(td4);
        fileTr.appendChild(td5);
        fileTr.appendChild(td6);
        fileTbody.appendChild(fileTr);
    }
}

//下载
function downloadFile(fileId) {
    var url = "/cykspark/file/downloadFile";
    window.location.href=url+"?fileId="+fileId;
}

//删除
function deleteFile(fileId) {
    bootbox.confirm("确定要删除吗?", function (result) {
        if (result) {
            var url = "/cykspark/file/deleteFile";
            $.ajax({
                url: url,
                dataType: 'json',
                data: {
                    fileId: fileId
                },
                success: function () {
                    showFileList();
                }
            });

        }
    });
}